package sort;

import java.util.Arrays;
import java.util.Random;
import java.util.Stack;

public class Sort4 {
    public static void random(int[] nums) {
        Random random = new Random();
        for (int i = 0; i < nums.length; i++) {
            nums[i] = random.nextInt(50);
        }
    }

    // 插入排序
    public static void insertSort(int[] nums) {
        for (int i = 1; i < nums.length; i++) {
            // 抽牌
            int tmp = nums[i];
            int j = i - 1;
            for (; j >= 0; j--) {
                // 遍历前面的牌, 放牌
                if(tmp < nums[j]) {
                    nums[j+1] = nums[j];
                }else {
                    break;
                }
            }
            nums[j + 1] = tmp;
        }
    }

    // 2.希尔排序
    public static void shellSort(int[] nums) {
        // 给数组分组
        int gap = nums.length;
        while(gap >= 1) {
            gap /= 2;
            shell(nums, gap);
        }
    }

    public static void shell(int[] nums, int gap) {
        for (int i = gap; i < nums.length; i++) {
            int tmp = nums[i];
            int j = i - gap;
            for (; j >= 0; j -= gap) {
                if (tmp < nums[j]) {
                    nums[j + gap] = nums[j];
                }else {
                    break;
                }
            }
            nums[j + gap] = tmp;
        }
    }

    // 3.选择排序
    public static void selectSort(int[] nums) {
        for (int i = 0; i < nums.length - 1; i++) {
            int min = i;
            for (int j = i + 1; j < nums.length; j++) {
                if (nums[j] < nums[min]) {
                    min = j;
                }
            }
            swap(nums, min, i);
        }
    }

    public static void swap(int[] nums, int i, int j) {
        int tmp = nums[i];
        nums[i] = nums[j];
        nums[j] = tmp;
    }

    // 4.堆排序
    public static void heapSort(int[] nums) {
        creatHeap(nums);

        // 堆的删除逻辑
        int size = nums.length;
        while (size != 0) {
            swap(nums, 0, size - 1);
            size--;
            downward(nums, 0, size);
        }
    }

    // 构建大根堆
    private static void creatHeap(int[] nums) {
        int size = nums.length;
        for (int parent = (size - 1 - 1) / 2; parent >= 0; parent--) {
            downward(nums, parent, size);
        }
    }

    private static void downward(int[] nums, int parent, int size) {
        int child = 2 * parent + 1;
        while (child < size) {
            // 选择孩子节点中较大者
            if (child + 1 < size && nums[child + 1] > nums[child]) {
                child = child + 1;
            }

            // 交换节点
            if (nums[child] > nums[parent]) {
                swap(nums, child, parent);
                parent = child;
                child = 2 * parent + 1;
            }else {
                break;
            }
        }
    }

    // 5.冒泡排序
    public static void bubbleSort(int[] nums) {
        // 比较的轮次
        for (int i = 0; i < nums.length - 1; i++) {
            // 两两比较, 较大值冒在最后
            for (int j = 0; j < nums.length - 1 - i; j++) {
                if (nums[j] > nums[j + 1]) {
                    swap(nums, j, j+1);
                }
            }
        }
    }

    // 快排
    public static void quickSort(int[] nums) {
        hoare(nums, 0, nums.length - 1);
    }

    private static void hoare(int[] nums, int left, int right) {
        if (left >= right) {
            return;
        }

        int pivotIndex = partition(nums, left, right);
        // [left, pivotIndex - 1]  [pivotIndex + 1, right]
        hoare(nums, left, pivotIndex - 1);
        hoare(nums, pivotIndex + 1, right);
    }

    private static int partition(int[] nums, int left, int right) {
        int pivot = new Random().nextInt(right - left + 1) + left;
        swap(nums, left, pivot);

        pivot = nums[left];
        int pivotIndex = left;
        while (left < right) {
            while (left < right && nums[right] >= pivot) {
                right--;
            }
            while (left < right && nums[left] <= pivot) {
                left++;
            }
            swap(nums, left, right);
        }
        swap(nums, pivotIndex, left);
        return left;
    }

    // 快排非递归
    public static void quickSortNor(int[] nums) {
        int begin = 0, end = nums.length - 1;
        Stack<Integer> stack = new Stack<>();
        int pivot = partition(nums, begin, end);
        if (pivot - 1 > begin) {
            // 先入左端点, 再入右端点
            stack.push(begin);
            stack.push(pivot - 1);
        }
        if (pivot + 1 < end) {
            stack.push(pivot + 1);
            stack.push(end);
        }

        while (!stack.isEmpty()) {
            end = stack.pop();
            begin = stack.pop();
            pivot = partition(nums, begin, end);
            if (pivot - 1 > begin) {
                // 先入左端点, 再入右端点
                stack.push(begin);
                stack.push(pivot - 1);
            }
            if (pivot + 1 < end) {
                stack.push(pivot + 1);
                stack.push(end);
            }
        }
    }

    // 归并
    public static void mergeSort(int[] nums) {
        merge(nums, 0, nums.length - 1);
    }

    private static void merge(int[] nums, int left, int right) {
        if (left >= right) {
            return;
        }

        int mid = left + ((right - left) >> 1);
        // [left, mid]  [mid + 1, right]
        merge(nums, left, mid);
        merge(nums, mid + 1, right);

        // 合并两个有序数组
        int[] tmp = new int[right - left + 1];
        int cur1 = left, cur2 = mid + 1, i = 0;
        while (cur1 <= mid && cur2 <= right) {
            tmp[i++] = nums[cur1] < nums[cur2] ? nums[cur1++] : nums[cur2++];
        }

        while (cur1 <= mid) {
            tmp[i++] = nums[cur1++];
        }
        while (cur2 <= right) {
            tmp[i++] = nums[cur2++];
        }

        for (int j = 0; j < right - left + 1; j++) {
            nums[j + left] = tmp[j];
        }
    }

    public static void mergeSortNor(int[] nums) {
        int gap = 1;
        int n = nums.length;
        while (gap < n) {
            for (int i = 0; i < n; i += 2*gap) {
                int left = i;
                int mid = left + gap - 1;
                int right = mid + gap;

                if (mid >= n) {
                    mid = n - 1;
                }
                if (right >= n) {
                    right = n - 1;
                }

                // 合并两个有序数组
                int[] tmp = new int[right - left + 1];
                int cur1 = left, cur2 = mid + 1, j = 0;
                while (cur1 <= mid && cur2 <= right) {
                    tmp[j++] = nums[cur1] < nums[cur2] ? nums[cur1++] : nums[cur2++];
                }

                while (cur1 <= mid) {
                    tmp[j++] = nums[cur1++];
                }
                while (cur2 <= right) {
                    tmp[j++] = nums[cur2++];
                }

                for (int k = 0; k < right - left + 1; k++) {
                    nums[k + left] = tmp[k];
                }
            }
            gap *= 2;
        }
    }

    // 计数排序
    public static void countSort(int[] nums) {
        int maxValue = nums[0];
        int minValue = nums[0];
        for (int x : nums) {
            if (x > maxValue) {
                maxValue = x;
            }
            if (x < minValue) {
                minValue = x;
            }
        }

        int length = maxValue - minValue + 1;
        int[] count = new int[length];
        for (int x : nums) {
            count[x - minValue]++;
        }

        int index = 0;
        for (int i = 0; i < length; i++) {
            while (count[i] != 0) {
                nums[index] = i + minValue;
                index++;
                count[i]--;
            }
        }
    }

    public static void main(String[] args) {
        int[] nums = new int[10];
        random(nums);
        System.out.println("原数组: " + Arrays.toString(nums));
        countSort(nums);
        System.out.println("排序后: " + Arrays.toString(nums));
    }
}
